package com.sanploy.file.service.impl;

import com.sanploy.file.dto.FoldToType;
import com.sanploy.file.dto.SizeConfig;
import com.sanploy.file.enums.FileType;
import com.sanploy.file.service.FileService;
import com.sanploy.file.util.FileUtil;
import com.sanploy.file.util.ImageUtil;
import com.sanploy.file.vo.FileVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by zhu yingzhi on 2017/9/12.
 */
@Service
public class FileServiceImpl implements FileService {


    @Value("${file.fold.origin}")
    private String origin;

    @Value("${file.fold.practice}")
    private String practice;

    @Value("${file.fold.thumbnail}")
    private String thumbnail;



    @Override
    public void createFirstFold(String name) {
        FileUtil.creatFirstFold(name);
    }

    @Override
    public List<String> findFirstFold() {
        return FileUtil.getFirstFold();
    }

    @Override
    public List<FileVo> uploadFile(String projectName, String directoryName, MultipartHttpServletRequest fileRequest) {
        List<FileVo> fileList = new ArrayList<>();
        if (FileUtil.isContainsProject(projectName)) {
            fileRequest.getFileNames().forEachRemaining(System.out::println);
            fileRequest.getFileMap().forEach((key, value) -> {
                fileRequest.getFiles(key).forEach(file->{
                    fileList.add(FileUtil.uploadFile(file, projectName + File.separator + directoryName));
                });
            });
        }
        return fileList;
    }

    @Override
    public List<FileVo> uploadImageToJpgCompress(String projectName,
                                                 int width, int height, String compress,
                                                 MultipartHttpServletRequest multipartHttpServletRequest) {
        List<FileVo> fileList = new ArrayList<>();
        if (FileUtil.isContainsProject(projectName)) {
            multipartHttpServletRequest.getFileMap().forEach((key, value) -> {
                //添加图片并把图片的原图和裁剪后的图保存下来并返回
                Optional.ofNullable(FileUtil.uploadImgToJpg(value, projectName + File.separator + origin+File.separator)).
                        ifPresent((originFile) -> {
                                    fileList.add(originFile);
                                    fileList.add(new FileVo(FileType.PRACTICE, originFile.getOriginName(),
                                            ImageUtil.changeImgSize(originFile.getSavePath(), projectName +
                                                    File.separator + practice + File.separator + originFile.getNewName(), width, height)));
                                }
                        );

            });
        }
        return fileList;
    }

    @Override
    public List<FileVo> uploadImage(String projectName, String directoryName,
                                    MultipartHttpServletRequest multipartHttpServletRequest) {
        List<FileVo> fileList = new ArrayList<>();
        if (FileUtil.isContainsProject(projectName)) {
            multipartHttpServletRequest.getFileMap().forEach((key, value) -> {
                fileList.add(FileUtil.uploadImgToJpg(value, projectName + File.separator + directoryName));
            });
        }
        return fileList;
    }

    @Override
    public List<FileVo> shopShowImage(String projectName, MultipartHttpServletRequest multipartHttpServletRequest) {
        List<FileVo> fileList = new ArrayList<>();
        if (FileUtil.isContainsProject(projectName)) {
            multipartHttpServletRequest.getFileMap().forEach((key, value) -> {
                Optional.ofNullable(FileUtil.uploadShowImgToJpg(value, projectName + File.separator,
                        new FoldToType[]{
                                new FoldToType(FileType.ORIGIN, origin),
                                new FoldToType(FileType.PRACTICE, practice,
                                        SizeConfig.pointMap.get(FileType.PRACTICE).x,
                                        SizeConfig.pointMap.get(FileType.PRACTICE).y),
                                new FoldToType(FileType.THUMBNAIL, thumbnail,
                                        SizeConfig.pointMap.get(FileType.THUMBNAIL).x,
                                        SizeConfig.pointMap.get(FileType.THUMBNAIL).y),
                        })).ifPresent(fileList::addAll);
            });
        }
        return fileList;
    }


}
